<?php
/**
 * alltosun.com  FamilyController.Class.php
 * ============================================================================
 * 版权所有 (C) 2015 齐齐哈尔大学计算机与控制工程学院，并保留所有权利。
 * ============================================================================
 * $Author: 范志俊 (fanfzj@163.com) $
 * $Date: 2015-10-22 上午12:24:54 $
 */
namespace Admin\Controller;
use Think\Controller;

class ClassController extends AuthController {

    public function index()
    {
        $Family = M('Class');

        $total      = $Family->count();
        if( $total ){
            $perNum=25;
            $Page=new \Think\Page( $total , $perNum );

            $Page->setConfig( 'prev' , "上一页" );//上一页
            $Page->setConfig( 'next' , '下一页' );//下一页
            $Page->setConfig( 'first' , '首页' );//第一页
            $Page->setConfig( 'last' , "末页" );//最后一页
            $Page->setConfig( 'theme' , '%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%' );

            $show=$Page->show();

            $this->assign( 'total' , $total );
            $this->assign( 'page' , $show );

        }

        $class_list = $Family->order('Id asc')->limit($Page->firstRow.','.$Page->listRows)->select();

        $this->assign('class_list', $class_list);

        $this->display('class_list');
    }

    //添加属
    public function add()
    {
        $this->display('class_add');
    }

    //检测属是否存在
    public function check_title()
    {
        $title = I('name');
        $result = check_title('class', $title);
        if($result){
            $this->ajaxReturn(array('status' => 'n', 'info' => '该班级已经存在'));
        }else{
            $this->ajaxReturn(array('status' => 'y', 'info' => ''));
        }
    }

    //编辑类别
    public function edit()
    {
        $Id = I('id');
        $class_info = M('Class')->where(array('Id' => $Id))->find();

        $this->assign('class_info', $class_info);
        $this->display('class_add');
    }

    //保存数据
    public function save()
    {
        $id = I('id', 0);
        $info = M('Class')->where(array('Id' => $id))->find();

        $data['name']  = I('name', '');
        $data['major_id']=I('major_id','1');
        if($info){
            //更新操作
            $result = M('Class')->where(array('Id' => $id))->save($data);
            if($result || $result === 0){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '属修改成功,正在转跳到属列表'));
            }else if($result === FALSE){
                $this->ajaxReturn(array('status' => 'error', 'info' => '属修改失败'));
            }
        }else{
        	//入库操作
            $data['add_time'] = date('Y-m-d H:i:s', time());
            $result = M('Class')->add($data);
            if($result){
                $this->ajaxReturn(array('status' => 'ok', 'info' => '属添加成功,正在转跳到属列表'));
            }else if($result){
                $this->ajaxReturn(array('status' => 'error', 'info' => '属添加失败'));
            }
        }

    }

    public function del()
    {
        $id=I( 'id' );
        $result=M( 'Class' )->delete( $id );
        if($result){
            $data[ 'status' ]=1;
            $data[ 'data' ]='属删除成功';
        }else if($result){
            $data[ 'status' ]=0;
            $data[ 'data' ]='属删除失败,请刷新重试';
        }
        $this->ajaxReturn( $data );
    }

    //班级列表
    public function class_list()
    {
        $major_id=I("Id");
        if($major_id==0)
        {
            $data['status']=0;
            $data['data']=M("Class")->select();
        }
        else
        {
            $item['major_id']=$major_id;
            $result=M("Class")->where($item)->select();
            if(!empty($result))
            {
                $data['status']=0;
                $data['data']=$result;
            }
            else
            {
                $data['status']=1;
                $data['data']="该专业没有对应的班级";
            }
        }
        $this->ajaxReturn($data);
    }
}
